import TipDOM from './TipDOM';
import DrawPoint from './DrawChildren/DrawPoint';
import DrawPolyline from './DrawChildren/DrawPolyline';
import DrawPolygon from './DrawChildren/DrawPolygon';
import DrawRectangle from './DrawChildren/DrawRectangle';
import DrawCircle from './DrawChildren/DrawCircle';
import DrawPointHeight from './DrawChildren/DrawPointHeight';

export default class CesiumDraw {
    constructor(viewer) {
        // 提示dom
        this.tip = new TipDOM(viewer);
        this.viewer = viewer;
        // 实例化数组
        this.InstanceList = [
            {
                type: 'Point', // 类型
                class: DrawPoint, // 构造方法
                instance: null, // 当前实例
            },
            {
                type: 'Polyline',
                class: DrawPolyline,
                instance: null,
            },
            {
                type: 'Polygon',
                class: DrawPolygon,
                instance: null,
            },
            {
                type: 'Rectangle',
                class: DrawRectangle,
                instance: null,
            },
            {
                type: 'Circle',
                class: DrawCircle,
                instance: null,
            },
            {
                type: 'PointHeight',
                class: DrawPointHeight,
                instance: null,
            },
        ];
    }
    // 效验参数
    vilidate(type) {
        const flag = this.InstanceList.find(item => item.type == type) ? true : false;
        if (!flag) {
            const typeList = this.InstanceList.map(item => item.type);
            console.error(`参数错误: CesiumDraw type 错误，必须为：${typeList.join(',')}`);
        }
        return flag;
    }
    /**
     * 开始绘制
     * @param {*} type
     * @param {* 是否显示测量Label } showMeasure
     */
    drawPromise(type, showMeasure) {
        if (!this.vilidate(type)) return;
        const instanceObj = this.getDrawInstanceObj(type);
        return instanceObj.instance.drawPromise(showMeasure); // 开始绘制
    }
    /**
     * 开始绘制
     * @param {*} type
     * @param {* 是否显示测量Label } showMeasure
     * @param {* 是否连续绘制 } continuousDraw
     */
    startDraw(type, showMeasure, continuousDraw) {
        if (!this.vilidate(type)) return;
        const instanceObj = this.getDrawInstanceObj(type);
        instanceObj.instance.startDraw(showMeasure); // 开始绘制
    }
    /**
     * 结束绘制
     * @param {*} type
     */
    endDraw(type) {
        if (!this.vilidate(type)) return;
        const instanceObj = this.getDrawInstanceObj(type);
        instanceObj.instance.endDraw(); // 结束绘制
    }
    /**
     * 清空 type 类型绘制
     * @param {*} type
     */
    removeDraw(type) {
        if (!this.vilidate(type)) return;
        const instanceObj = this.getDrawInstanceObj(type);
        instanceObj.instance.removeDraw(); // 结束绘制
    }
    /**
     * 清空所有绘制
     */
    removeAllDraw() {
        this.InstanceList.forEach(instanceObj => {
            if (instanceObj.instance) instanceObj.instance.removeDraw();
        });
    }
    // 获取绘制实例
    getDrawInstanceObj(type) {
        const instanceObj = this.InstanceList.find(item => item.type == type);
        if (!instanceObj.instance) {
            instanceObj.instance = new instanceObj.class(this.viewer, this.tip);
        }
        return instanceObj;
    }
    getDraw(type) {
        if (!this.vilidate(type)) return;
        const instanceObj = this.getDrawInstanceObj(type);
        const entityList = instanceObj.instance.getAllEntity(); // 结束绘制
        return entityList;
    }
}
